package com.drug.wms.service.impl;

import com.drug.common.utils.DateUtils;
import com.drug.wms.domain.BusSkuStock;
import com.drug.wms.domain.BusTransferGoods;
import com.drug.wms.domain.BusTransferGoodsInfo;
import com.drug.wms.mapper.BusTransferGoodsInfoMapper;
import com.drug.wms.mapper.BusTransferGoodsMapper;
import com.drug.wms.page.BusTransferGoodsPage;
import com.drug.wms.service.GenerateSequence;
import com.drug.wms.service.IBusSkuStockService;
import com.drug.wms.service.IBusTransferGoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * 商品调货单Service业务层处理
 *
 * @author ruoyi
 * @date 2021-12-01
 */
@Service
public class BusTransferGoodsServiceImpl implements IBusTransferGoodsService {
    @Autowired
    private BusTransferGoodsMapper busTransferGoodsMapper;
    @Autowired
    private BusTransferGoodsInfoMapper busTransferGoodsInfoMapper;

    /**
     * 查询商品调货单
     *
     * @param id 商品调货单主键
     * @return 商品调货单
     */
    @Override
    public BusTransferGoodsPage selectBusTransferGoodsById(String id) {
        BusTransferGoodsPage busTransferGoodsPage = new BusTransferGoodsPage();
        BusTransferGoods busTransferGoods = busTransferGoodsMapper.selectBusTransferGoodsById(id);
        busTransferGoodsPage.setBusTransferGoods(busTransferGoods);

        BusTransferGoodsInfo busTransferGoodsInfo = new BusTransferGoodsInfo();
        busTransferGoodsInfo.setTranferId(id);
        List<BusTransferGoodsInfo> busTransferGoodsInfos = busTransferGoodsInfoMapper.selectBusTransferGoodsInfoList(busTransferGoodsInfo);
        busTransferGoodsPage.setBusTransferGoodsInfoList(busTransferGoodsInfos);

        return busTransferGoodsPage;
    }

    /**
     * 查询商品调货单列表
     *
     * @param busTransferGoods 商品调货单
     * @return 商品调货单
     */
    @Override
    public List<BusTransferGoods> selectBusTransferGoodsList(BusTransferGoods busTransferGoods) {
        return busTransferGoodsMapper.selectBusTransferGoodsList(busTransferGoods);
    }

    /**
     * 新增商品调货单
     *
     * @param busTransferGoodsPage 商品调货单
     * @return 结果
     */
    @Override
    @Transactional
    public int insertBusTransferGoods(BusTransferGoodsPage busTransferGoodsPage) {
        BusTransferGoods busTransferGoods = busTransferGoodsPage.getBusTransferGoods();
        busTransferGoods.setCreateTime(DateUtils.getNowDate());
        busTransferGoods.setId(null);
        busTransferGoods.setSaleNo(GenerateSequence.genSeqNo("DH", 10));
        busTransferGoodsMapper.insertBusTransferGoods(busTransferGoods);

        List<BusTransferGoodsInfo> busTransferGoodsInfoList = busTransferGoodsPage.getBusTransferGoodsInfoList();
        for (BusTransferGoodsInfo b : busTransferGoodsInfoList) {
            b.setId(null);
            b.setTranferId(busTransferGoods.getId());
            busTransferGoodsInfoMapper.insertBusTransferGoodsInfo(b);
        }
        return 1;
    }

    /**
     * 修改商品调货单
     *
     * @param busTransferGoodsPage 商品调货单
     * @return 结果
     */
    @Override
    public int updateBusTransferGoods(BusTransferGoodsPage busTransferGoodsPage) {
        BusTransferGoods busTransferGoods = busTransferGoodsPage.getBusTransferGoods();
        busTransferGoods.setUpdateTime(DateUtils.getNowDate());
        busTransferGoodsMapper.updateBusTransferGoods(busTransferGoods);

        busTransferGoodsInfoMapper.deleteBusTransferGoodsInfoByTransferId(busTransferGoods.getId());
        List<BusTransferGoodsInfo> busTransferGoodsInfoList = busTransferGoodsPage.getBusTransferGoodsInfoList();
        for (BusTransferGoodsInfo b : busTransferGoodsInfoList) {
            b.setId(null);
            b.setTranferId(busTransferGoods.getId());
            busTransferGoodsInfoMapper.insertBusTransferGoodsInfo(b);
        }

        return 1;
    }

    /**
     * 批量删除商品调货单
     *
     * @param ids 需要删除的商品调货单主键
     * @return 结果
     */
    @Override
    public int deleteBusTransferGoodsByIds(String[] ids) {
        busTransferGoodsMapper.deleteBusTransferGoodsByIds(ids);
        for (String id : ids) {
            busTransferGoodsInfoMapper.deleteBusTransferGoodsInfoByTransferId(id);
        }
        return 1;
    }

    /**
     * 删除商品调货单信息
     *
     * @param id 商品调货单主键
     * @return 结果
     */
    @Override
    public int deleteBusTransferGoodsById(String id) {
        busTransferGoodsMapper.deleteBusTransferGoodsById(id);
        busTransferGoodsInfoMapper.deleteBusTransferGoodsInfoByTransferId(id);
        return 1;
    }

    @Autowired
    private IBusSkuStockService iBusSkuStockService;

    /**
     * 审批当前调货单
     *
     * @param id
     */
    @Override
    public void approveTransfer(String id) {
        //1.查询当前调货单
        BusTransferGoodsPage busTransferGoodsPage = this.selectBusTransferGoodsById(id);
        BusTransferGoods busTransferGoods = busTransferGoodsPage.getBusTransferGoods();
        List<BusTransferGoodsInfo> busTransferGoodsInfoList = busTransferGoodsPage.getBusTransferGoodsInfoList();
        //移除的库存
        List<BusSkuStock> busSkuStockListRemove = new ArrayList<>();
        //增加的库存
        List<BusSkuStock> busSkuStockListAdd = new ArrayList<>();

        for (BusTransferGoodsInfo b : busTransferGoodsInfoList) {
            //减库存
            BusSkuStock busSkuStock = new BusSkuStock();
            busSkuStock.setSkuId(b.getGoodsNo());
            busSkuStock.setWarehouseNo(b.getWarehouseNo());
            busSkuStock.setBatchNo(b.getBatchNo());
            busSkuStock.setAmount(new BigDecimal(b.getQuantity()).negate());
            busSkuStock.setCanSaleAmount(new BigDecimal(b.getQuantity()).negate());
            busSkuStockListRemove.add(busSkuStock);
            //加库存
            BusSkuStock busSkuStock2 = new BusSkuStock();
            busSkuStock.setSkuId(b.getGoodsNo());
            busSkuStock.setWarehouseNo(busTransferGoods.getWarehouseNo());
            busSkuStock.setBatchNo(b.getBatchNo());
            busSkuStock.setAmount(new BigDecimal(b.getQuantity()));
            busSkuStock.setCanSaleAmount(new BigDecimal(b.getQuantity()));
            busSkuStockListAdd.add(busSkuStock2);
        }
        iBusSkuStockService.insertOrUpdateSkuStock(busSkuStockListRemove);
        iBusSkuStockService.insertOrUpdateSkuStock(busSkuStockListAdd);
    }
}
